package cn.hh.modules.system.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import cn.hh.modules.system.dto.input.UserQueryPara;
import cn.hh.modules.system.dto.model.ButtonVO;
import cn.hh.modules.system.dto.model.MenuVO;
import cn.hh.modules.system.dto.model.UserInfoVO;
import cn.hh.modules.system.entity.Menu;
import cn.hh.modules.system.entity.Role;
import cn.hh.modules.system.entity.RoleMenu;
import cn.hh.modules.system.entity.User;
import cn.hh.modules.system.mapper.RoleMenuMapper;
import cn.hh.modules.system.mapper.UserMapper;
import cn.hh.modules.system.mapper.UserRoleMapper;
import cn.hh.modules.system.service.IUserService;
import cn.hh.utils.TreeBuilder;
import cn.hutool.core.bean.BeanUtil;

/**
 * <p> 系统管理-用户基础信息表 服务实现类 </p>
 *
 * @author: hunagh
 * @date: 2021-06-18
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService{
	
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	
	@Override
	public UserInfoVO getCurrentUserInfo(String token) {
		User user = userMapper.getUserInfoByToken(token);
		UserInfoVO userInfoVO = new UserInfoVO();
		BeanUtil.copyProperties(user, userInfoVO);
		
		Set<String> roles = new HashSet();
		Set<MenuVO> menuVOS = new HashSet();
		Set<ButtonVO> buttonVOS = new HashSet();
		
		//查询某个用户的角色
		List<Role> roleList = userRoleMapper.selectRoleByUserId( user.getId());
		if(roleList != null && !roleList.isEmpty()) {
			roles.add(roleList.get(0).getCode());
			
		}
		//查询某个角色的菜单
		List<Menu> menuList = roleMenuMapper.selectMenusByRoleId(roleList.get(0).getId());
		if(menuList != null && !menuList.isEmpty()) {
			menuList.stream().filter(Objects::nonNull).forEach(menu -> {
				if("button".equals(menu.getType().toLowerCase())) {
					//如果权限是按钮，就添加到按钮里面
					ButtonVO buttonVO = new ButtonVO();
					BeanUtil.copyProperties(menu, buttonVO);
					buttonVOS.add(buttonVO);
				}
				if("menu".equals(menu.getType().toLowerCase())) {
					//如果权限是菜单，就添加到菜单里面
					MenuVO menuVO = new MenuVO();
					BeanUtil.copyProperties(menu, menuVO);
					menuVOS.add(menuVO);
				}
			});
		}
		userInfoVO.getRoles().addAll(roles);
		userInfoVO.getButtons().addAll(buttonVOS);
		userInfoVO.getMenus().addAll(TreeBuilder.buildTree(menuVOS));
		return userInfoVO;
	}

	@Override
	public void listPage(Page<User> page, UserQueryPara filter) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Integer save(User input) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer updatePersonalInfo(User para) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> list(UserQueryPara filter) {
		// TODO Auto-generated method stub
		return null;
	}



}
